layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    //分类管理
    table.render({
        elem: '#LAY-app-course-category'
        , url: layui.setter.host + '/course_category'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'name', title: '分类名', minWidth: 100}
            , {field: 'icon', title: '图标'}
            , {field: 'sort', title: '排序'}
            , {field: 'status', title: '状态', templet: '#buttonTpl', minWidth: 80, align: 'center'}
            , {field: 'create_time', title: '创建时间'}
            , {field: 'update_time', title: '更新时间'}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-course-category'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-course-category)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确认删除吗？', function (index) {
                admin.req({
                    url: layui.setter.host + '/course_category/' + data.id,
                    type: "delete",
                    success: function (res) {
                        if (res.code === 0) {
                            obj.del();
                            layer.msg(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 5});
                        }
                        layer.close(index);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑分类'
                , area: ['500px', '450px']
                , id: 'LAY-app-course-category-edit'
                , success: function (layero, index) {
                    view(this.id).render('app/course/categoryform', data).done(function () {
                        form.render(null, 'LAY-app-course-category-form');

                        //监听提交
                        form.on('submit(LAY-app-course-category-submit)', function (form) {
                            admin.req({
                                url: layui.setter.host + '/course_category/' + data.id
                                , data: form.field
                                , type: "put"
                                , done: function (res) {
                                    if (!res.code) {
                                        layer.msg(res.message, {icon: 6});
                                        obj.update(form.field);
                                    } else {
                                        layer.msg(res.message, {icon: 5});
                                    }
                                    layer.close(index);
                                }
                            });
                        });
                    });
                }
            });
        }
    });

    //课程管理
    table.render({
        elem: '#LAY-app-course-list'
        , url: layui.setter.host + '/course'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', sort: true}
            , {field: 'name', title: '课程分类', templet: '<div>{{ d.category.name }}</div>'}
            , {field: 'title', title: '课程名称'}
            , {field: 'price', title: '价格', width: 100}
            , {field: 'status', title: '发布状态', templet: '#buttonTpl', width: 100, align: 'center'}
            , {field: 'update_time', title: '发布时间'}
            , {title: '操作', minWidth: 280, align: 'center', fixed: 'right', toolbar: '#table-course-list'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-course-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此课程？', function (index) {
                admin.req({
                    url: layui.setter.host + '/course/' + data.id,
                    type: "delete",
                    success: function (res) {
                        if (res.code === 0) {
                            obj.del();
                            layer.msg(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 5});
                        }
                        layer.close(index);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑课程'
                , area: ['1000px', '600px']
                , id: 'LAY-app-course-edit'
                , success: function (layero, index) {
                    view(this.id).render('app/course/listform', data).done(function () {
                        form.render(null, 'LAY-app-course-form');

                        console.log(data)

                        ue.ready(function () {
                            //设置内容
                            ue.setContent(data.content);
                        });

                        //监听提交
                        form.on('submit(LAY-app-course-submit)', function (res) {

                            //获取内容
                            res.field.content = ue.getContent();

                            admin.req({
                                url: layui.setter.host + '/course/' + data.id
                                , data: res.field
                                , type: "put"
                                , done: function (res) {
                                    if (!res.code) {
                                        layer.msg(res.message, {icon: 6});
                                        obj.update(form.field);
                                    } else {
                                        layer.msg(res.message, {icon: 5});
                                    }
                                    layer.close(index);
                                }
                            });
                        });
                    });
                }
            });
        }
    });

    //章节管理
    table.render({
        elem: '#LAY-app-course-lesson'
        , url: layui.setter.host + '/course/' + layui.router().search.course_id + '/lesson'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', sort: true}
            , {field: 'title', title: '章节名称'}
            , {field: 'type', title: '章节类型', templet: '#typeTpl', width: 90, align: 'center'}
            , {field: 'is_free', title: '试听章节', templet: '#freeTpl', width: 90, align: 'center'}
            , {field: 'status', title: '章节状态', templet: '#buttonTpl', width: 90, align: 'center'}
            , {field: 'update_time', title: '发布时间'}
            , {title: '操作', minWidth: 180, align: 'center', fixed: 'right', toolbar: '#table-course-lesson'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-course-lesson)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此章节？', function (index) {
                admin.req({
                    url: layui.setter.host + '/course/' + data.course_id + '/lesson/' + data.id,
                    type: "delete",
                    success: function (res) {
                        if (res.code === 0) {
                            obj.del();
                            layer.msg(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 5});
                        }
                        layer.close(index);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑章节'
                , area: ['1000px', '600px']
                , id: 'LAY-app-course-lesson-edit'
                , success: function (layero, index) {
                    view(this.id).render('app/course/lessonform', data).done(function () {
                        form.render(null, 'LAY-app-course-lesson-form');

                        ue.ready(function () {
                            //设置内容
                            ue.setContent(data.content);
                        });

                        //监听提交
                        form.on('submit(LAY-app-course-lesson-submit)', function (res) {
                            //获取内容
                            res.field.content = ue.getContent();

                            admin.req({
                                url: layui.setter.host + '/course/' + data.course_id + '/lesson/' + data.id
                                , data: res.field
                                , type: "put"
                                , done: function (res) {
                                    if (!res.code) {
                                        layer.msg(res.message, {icon: 6});
                                        obj.update(form.field);
                                    } else {
                                        layer.msg(res.message, {icon: 5});
                                    }
                                    layer.close(index);
                                }
                            });
                        });
                    });
                }
            });
        }
    });

    //课件管理
    table.render({
        elem: '#LAY-app-course-document'
        , url: layui.setter.host + '/course/' + layui.router().search.course_id + '/document'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', sort: true}
            , {field: 'title', title: '课件名称'}
            , {field: 'filepath', title: '课件路径', templet: '#typeTpl'}
            , {field: 'sort', title: '排序', width: 60, align: 'center'}
            , {field: 'update_time', title: '发布时间', width: 160}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-course-document'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-course-document)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此章节？', function (index) {
                admin.req({
                    url: layui.setter.host + '/course/' + data.course_id + '/document/' + data.id,
                    type: "delete",
                    success: function (res) {
                        if (res.code === 0) {
                            obj.del();
                            layer.msg(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 5});
                        }
                        layer.close(index);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑课件'
                , area: ['700px', '400px']
                , id: 'LAY-app-course-document-edit'
                , success: function (layero, index) {
                    view(this.id).render('app/course/documentform', data).done(function () {
                        form.render(null, 'LAY-app-course-document-form');

                        //监听提交
                        form.on('submit(LAY-app-course-document-submit)', function (res) {
                            admin.req({
                                url: layui.setter.host + '/course/' + data.course_id + '/document/' + data.id
                                , data: res.field
                                , type: "put"
                                , done: function (res) {
                                    if (!res.code) {
                                        layer.msg(res.message, {icon: 6});
                                        obj.update(form.field);
                                    } else {
                                        layer.msg(res.message, {icon: 5});
                                    }
                                    layer.close(index);
                                }
                            });
                        });
                    });
                }
            });
        }
    });

    exports('course', {})
});
